﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;
using GnToolMain.Layout;

namespace GnToolMain
{
    public abstract class Pane : Grid
    {
		XmlWriter mXmlWriter = null;
		XmlReader mXmlReader = null; 
        protected readonly List<ContentControl> mControls = new List<ContentControl>();
        protected List<ContentControl> Controls
        {
            get { return mControls; }
        }

        public Pane()
        {
			this.Loaded += new System.Windows.RoutedEventHandler( this.LoadedUmsg );
        }

		~Pane()
		{
		}

		private void LoadedUmsg( object sender, RoutedEventArgs e )
		{
			Init();
		}

		private void ClosingUmsg( object sender, System.ComponentModel.CancelEventArgs e )
		{
			Release();
		}

		private void Init()
		{
			SetClosingMassage();
			CreateLoadLayoutXml();
			if( mXmlReader != null )
				LoadControlLayout();
		}

		private void SetClosingMassage()
		{
			Window parentWindow = null;
			FrameworkElement parent = this.Parent as FrameworkElement;
			while( parent != null )
			{
				parentWindow = parent as Window;
				if( parentWindow != null )
					break;
				parent = parent.Parent as FrameworkElement;
			}

			if( parentWindow != null )
				parentWindow.Closing += new System.ComponentModel.CancelEventHandler( ClosingUmsg );
		}

		private void Release()
		{
			CreateSaveLayoutXml();
			if( mXmlWriter != null )
				SaveControlLayout();
		}

		private void CreateLoadLayoutXml()
		{
			try
			{
				String layoutName = Name;
				if( layoutName.Length <= 0 )
					layoutName = ToString();
				layoutName = layoutName.Replace( '.', '_' );

				String readName = GnControlManager.LayoutFolder + layoutName + ".xml";
				mXmlReader = XmlReader.Create( readName );
			}
			catch //(System.Exception ex)
			{	
			}
		}

		private void CreateSaveLayoutXml()
		{
			try
			{
				String layoutName = Name;
				if( layoutName.Length <= 0 )
					layoutName = ToString();
				layoutName = layoutName.Replace( '.', '_' );

				String readName = GnControlManager.LayoutFolder + layoutName + ".xml";
				mXmlWriter = XmlWriter.Create( readName );
			}
			catch //(System.Exception ex)
			{
			}
		}

		public bool SaveControlLayout()
		{
            mXmlWriter.WriteStartElement( "Main" );
            LayoutManager.SaveXml( mXmlWriter, this.Children) ;
            mXmlWriter.WriteEndElement();

            mXmlWriter.Close();
			return true;
		}

		public bool LoadControlLayout()
		{
			LayoutManager.LoadXml( mXmlReader, this.Children );
            mXmlReader.Close();
			return true;
		}
    }
}
